<?php
//header("Expires: Sat, 1 Jan 2005 00:00:00 GMT");
//header("Last-Modified: ". gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

include("dbconn.php");

if (isset($_GET["logout"]))
{
	session_write_close();
	header("Location: ../beheer/");
}

if (isset($_GET["addweek"]))
{
	
	if (isset($_GET["weeknr"]) && isset($_GET["basis"]))
	{
		$QUERY = "
			SELECT DISTINCT week 
			FROM studieuur
			ORDER BY week DESC
			LIMIT 1
		";
		$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br /><br />' . mysql_error());
		//echo "query = $QUERY <br /><br />";
		
		$weeknr = mysql_result($RESULT, 0, "week");
		$weeknr >= 52 ? $weeknr = 1 : $weeknr++;
		
		$QUERY = "
			INSERT INTO studieuur (id, uur, dag, week) 
			VALUES (NULL, '3', 'Di', '$weeknr');";
		$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br /><br />' . mysql_error());
		//echo "query = $QUERY <br /><br />week toegevoegd!";
	}
	else
	{



?><table cellspacing="0" align="left" valign="top">
	<tr>
		<td colspan="<?php print($DBC->rows); ?>">
			<h1>Voeg toe: week</h1>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td>
			nr:
		</td>
		<td>
			<input type="text" style="display: block; width: 100px;" />
		</td>
	</tr>
	<tr style="vertical-align: top;" height="100">
		<td>
			basis:
		</td>
		<td>
			<select style="display: block; width: 100px;">
				<option>b1</option>
				<option>b2</option>
			</select>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td colspan="2" align="center">
			<a onclick="" class="bigbutton">voeg&nbsp;toe!</a>
		</td>
	</tr>
</table>
<?



	}
	
}

if (isset($_GET["delweek"]) && isset($_GET["week"]))
{
	$QUERY = "
		DELETE FROM studieuur 
		WHERE week = " . $_GET["week"];
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br /><br />' . mysql_error());
	//echo "query = $QUERY \n\n er gebeurt al iets";
}

if (isset($_GET["editweek"]))
{
	$QUERY = "";
	//echo "query = $QUERY \n\n er gebeurt nog niets";
}

if (isset($_GET["editweekdefault"]))
{
	$QUERY = "";
	//echo "query = $QUERY \n\n er gebeurt nog niets";
}

if (isset($_GET["addplace"]) && isset($_GET["class"]) && isset($_GET["placenr"]))
{
	$class = $_GET["class"];
	$placenr = $_GET["placenr"];
	
	$QUERY = "
		INSERT INTO leerlingtoegang (plaatsnr, klas) 
		VALUES ('$placenr', '$class');
	";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	////echo "$QUERY<br />";
	
}

if (isset($_GET["addplace"]) && isset($_GET["hourid"]) && isset($_GET["placenr"]))
{
	$hourid = $_GET["hourid"];
	$placenr = $_GET["placenr"];
	
	$QUERY = "
		INSERT INTO studieuurplaats (plaatsnr, studieuur) 
		VALUES ('$placenr', '$hourid');
	";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	////echo "$QUERY<br />";
	
}

if (isset($_GET["addhour"]) && isset($_GET["values"]) && isset($_GET["hourid"]))
{
	$hourid = $_GET["hourid"];
	$values = str_replace("\'", "'", $_GET["values"]);
	
	$QUERY = "
		INSERT INTO plaats (plaatsnr, lokaal, docentid, vak, maxleerlingen) 
		VALUES (NULL, $values 30)
		";
	////echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
	$QUERY = "
		SELECT plaatsnr
		FROM plaats
		ORDER BY plaatsnr DESC
		LIMIT 1
		";
	////echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
	$plaatsnr = mysql_result($RESULT, 0, "plaatsnr");
	
	$QUERY = "
		INSERT INTO studieuurplaats (plaatsnr, studieuur) 
		VALUES ($plaatsnr, $hourid)
		";
	////echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
}

if (isset($_GET["addhour"]) && $_GET["addhour"] == "new")
{



?><table cellspacing="0" align="left" valign="top">
	<tr>
		<td colspan="<?php print($DBC->rows); ?>">
			<h1>Voeg toe: uur</h1>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td>
			week:
		</td>
		<td>
			<select style="display: block; width: 100px;">
				<option>52</option>
				<option>53</option>
			</select>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td>
			dag:
		</td>
		<td>
			<select style="display: block; width: 100px;">
				<option>Ma</option>
				<option>Di</option>
				<option>Wo</option>
				<option>Do</option>
				<option>Vr</option>
			</select>
		</td>
	</tr>
	<tr style="vertical-align: top;" height="100">
		<td>
			uur:
		</td>
		<td>
			<select style="display: block; width: 100px;">
				<option>0</option>
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
				<option>6</option>
				<option>7</option>
				<option>8</option>
			</select>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td colspan="2" align="center">
			<a onclick="" class="bigbutton">voeg&nbsp;toe!</a>
		</td>
	</tr>
</table>
<?php


}

if (isset($_GET["delhour"]) && isset($_GET["values"]))
{
	$hourid = $_GET["hourid"];
	$values = str_replace("\'", "'", $_GET["values"]);
	
	$QUERY = "
		SELECT plaatsnr
		FROM plaats
		WHERE lokaal IN ($values '')
		AND docentid IN ($values '')
		AND vak IN ($values '')
	";
	//echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
	$placenr = mysql_result($RESULT, 0, "plaatsnr");
	
	$QUERY = "
		DELETE FROM plaats
		WHERE plaatsnr = '$placenr'
		";
	//echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
	$QUERY = "
		DELETE FROM studieuurplaats (plaatsnr, studieuur) 
		WHERE plaatsnr = '$placenr'
		";
	//echo "QUERY = $QUERY <br /><br />";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	
}

if (isset($_GET["delplace"]) && isset($_GET["class"]) && isset($_GET["placenr"]))
{
	$class = $_GET["class"];
	$placenr = $_GET["placenr"];
	
	$QUERY = "
		DELETE FROM leerlingtoegang 
		WHERE plaatsnr = '$placenr'
		AND klas = '$class'
	";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	//echo "$QUERY<br />";
	
}

if (isset($_GET["delplace"]) && isset($_GET["hourid"]) && isset($_GET["placenr"]))
{
	$hourid = $_GET["hourid"];
	$placenr = $_GET["placenr"];
	
	$QUERY = "
		DELETE FROM studieuurplaats
		WHERE plaatsnr = '$placenr'
		AND	studieuur = '$hourid'
	";
	$RESULT = mysql_query($QUERY) or die('<b style="color: red; font-size: 25px;">Error!</b><br />QUERY:<br />' . $QUERY . '<br /><br />' . mysql_error());
	//echo "$QUERY<br />";
	
}

if (isset($_GET["editpupils"]))
{
	$QUERY = "";
	//echo "query = $QUERY \n\n er gebeurt nog niets";
}

if (isset($_GET["selectweek"]) && isset($_GET["week"]))
{
	$QUERY = "
		SELECT dag, uur, id 
		FROM studieuur 
		WHERE week = '" . $_GET["week"] . "'";
	$resultuurnr = mysql_query($QUERY);
?>
					<table align="center">
						<tr>
<?php for($i = 0; $i < mysql_num_rows($resultuurnr); $i++) { 
?>							<td><a id="su_<?php print(mysql_result($resultuurnr, $i, "id"));?>" onclick="gethours('<?php print(mysql_result($resultuurnr, $i, "id")); ?>')" href="#" class="wk_button"><?php print(mysql_result($resultuurnr, $i, "dag") . mysql_result($resultuurnr, $i, "uur"));?></a></td>
<?php if ($i%4 == 0 && $i > 0) {
?>						</tr>
						<tr>
<?php } } 
?>						</tr>
					</table>
					<table align="right">
						<tr>
							<td><a onclick="addentirehour();" class="actionbutton"><img src="pics/addweek.png" /></a></td>
							<td><a onclick="delentirehour();" class="actionbutton"><img src="pics/delweek.png" /></a></td>
						</tr>
					</table>
				<?php
}

if (isset($_GET["getweeks"]) && isset($_GET["week"]))
{
	$weeknr = $_GET["week"];

	$QUERY = "
		SELECT DISTINCT studieuur 
		FROM intekenen";
	////echo $Qweeknr;
	$resultweeknr = mysql_query($QUERY);
	$studieuurArr = array();
	$aantalstudieuren = mysql_numrows($resultweeknr);
	////echo $aantalstudieuren;
	for($i=0; $i<$aantalstudieuren; $i++) //zet alle weeknrs in $studieuurArr[]
	{
		$studieuurArr[$i] = mysql_result($resultweeknr, $i, "studieuur");
		//$studieuurArr[$i] = substr($studieuurArr[$i], 0, 2);
	}
	
	$weeknrArr = array();
	for($i=0; $i < $aantalstudieuren; $i++)
	{
		if (!in_array($studieuurArr[$i], $weeknrArr))
		{$weeknrArr[sizeof($weeknrArr)] =	$studieuurArr[$i];}
	}
}

if (isset($_GET["gethours"]) && isset($_GET["id"]))
{
	$sid = $_GET["id"];
	$QUERY = "
		SELECT SK.klas, S.uur, S.dag, S.week
		FROM studieuur S, studieuurklas SK
		WHERE SK.studieuur  = S.id
		AND S.id = $sid
	";
	////echo $QUERY;
	$resultheader = mysql_query($QUERY) or die (mysql_error());

	$QUERY = "
		SELECT uur, dag, week
		FROM studieuur
		WHERE id = $sid
	";
	////echo $QUERY;
	$resulttitle = mysql_query($QUERY) or die (mysql_error());

	
?><table cellspacing="0" align="left" valign="top">
	<tr>
		<td colspan="<?php print(mysql_numrows($resultheader)); ?>">
			<h1>Week <?php print(mysql_result($resulttitle, 0, "week")); ?>,  <?php print(mysql_result($resulttitle, 0, "dag")); ?><?php print(mysql_result($resulttitle, 0, "uur")); ?>: <input id="reloadGethours_id" type="text" value="alle klassen" style="color: gray;" onfocus="if (this.value == 'alle klassen') { this.value = ''; this.style.color = 'black'; }" onblur="if (this.value == '') { this.value = 'alle klassen'; this.style.color = 'gray'; }" onkeyup="if (enter_was_pressed(event)) { reloadGethours_id(); }" /></h1>
		</td>
	</tr>
	<tr style="vertical-align: top;">
		<td id="gethours_id">
<?php


require("gethours_id.php");
	
	
?>		</td>
	</tr>
</table>
<?php


}

if (isset($_GET["gethours"]) && isset($_GET["week"]))
{
	$week = $_GET["week"];
	$QUERY = "
		SELECT COUNT( DISTINCT S.id ) AS colspan
		FROM studieuur S, studieuurklas SK
		WHERE SK.studieuur  = S.id
		AND S.week = '$week'
	";
	////echo $QUERY;
	$resultcolspan = mysql_query($QUERY);
	$columns = mysql_result($resultcolspan, 0, "colspan");


?><table cellspacing="0" align="left" valign="top">
	<tr>
		<td colspan="<?php print("$columns"); ?>">
			<h1>Week <?php print("$week"); ?>: <input id="reloadGethours_week" type="text" value="alle uren" style="color: gray;" onfocus="if (this.value == 'alle uren') { this.value = ''; this.style.color = 'black'; }" onblur="if (this.value == '') { this.value = 'alle uren'; this.style.color = 'gray'; }" onkeyup="if (enter_was_pressed(event)) { reloadGethours_week(); }" /></h1>
		</td>
	</tr>
	<tr>
		<td id="gethours_week">
<?php



require("gethours_week.php");
	
	
	
?>		</td>
	</tr>
</table>
<?php



}
mysql_close();



?>